;;; Copyright © 2021 Maxime Devos <maximedevos@telenet.be>
;;; This file is part of rehash.
;;;
;;; rehash is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; rehash is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with rehash.  If not, see <http://www.gnu.org/licenses/>.
(define-library (remirror embed-file)
  (export data-from-builddir)
  (import (only (rnrs syntax-case)
		syntax syntax-case syntax->datum)
	  (only (rnrs bytevectors)
		bytevector->u8-list)
	  (only (srfi srfi-4)
		u8vector)
	  (only (srfi srfi-98)
		get-environment-variable)
	  (only (rnrs io ports)
		get-bytevector-all)
	  (only (rnrs io simple)
		call-with-input-file)
	  (only (rnrs base)
		case begin define-syntax lambda let* apply
		string-append))
  (begin
    (define-syntax data-from-builddir
      (lambda (s)
	(syntax-case s ()
	  ((_ type text)
	   (let* ((name (syntax->datum #'text))
		  (builddir (get-environment-variable "builddir"))
		  (file-name (string-append builddir "/" name))
		  (data (call-with-input-file file-name
			  (lambda (port)
			    (get-bytevector-all port)))))
	     (case (syntax->datum #'type)
	       ((bytevector) data)
	       ((u8vector)
		(apply u8vector (bytevector->u8-list data)))))))))))
